﻿using EasyHttp.Http;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Text;

namespace EasyPayDemo.WeiPay
{
    /// <summary>
    /// 标准-付款码支付
    /// 被扫
    /// </summary>
    public class ExcuteScanPay : IFunction
    {
        public override void Execute()
        {
            string orgId = WeiPayConfig.orgId;

            M_ScanPay m_ScanPay = new M_ScanPay();
            m_ScanPay.orgId = WeiPayConfig.orgId;
            m_ScanPay.orgMercode = WeiPayConfig.orgMercode;
            m_ScanPay.orgTermno = WeiPayConfig.orgTermno;
            m_ScanPay.orgTrace = WeiPayUtils.GetOrgTrace(orgId);
            
            m_ScanPay.signType = WeiPayConfig.SIGN_TYPE_RSA256;
            #region data 处理

            M_ScanPay.Data dataBean = new M_ScanPay.Data();
            dataBean.authCode = "135907471006943270";
            //data.delaySettleFlag = "1";
            //data.infoAttach = "附加参数";
            //data.investor = "a";
            dataBean.orderInfo = "微信被扫订单10分";
            //data.orgPreferentialAmt = "1";
            //data.patnerSettleFlag = "1";
            //data.splitSettleFlag = "0";
            //data.timeoutMinutes = 60;
            dataBean.tradeAmt = 10;

            #endregion
            m_ScanPay.data = dataBean;

            #region sign 处理
            
            var chkStr=WeiPayUtils.GetSignData<M_ScanPay.Data>(dataBean);
            string sign = EncryptionUtil.RSASignJavaBouncyCastle(chkStr.ToString(),WeiPayConfig.PrivateKey, "SHA256withRSA");
            m_ScanPay.sign = sign;
            #endregion



            #region appendata 处理

            M_ScanPay.Appenddata appenddata = new M_ScanPay.Appenddata();
            appenddata.wxGoodsTag = "ok";
            appenddata.wxLimitPay = "no_credit";

            M_ScanPay.Identity identity = new M_ScanPay.Identity();
            //identity.encryptInfo = digital_envelope;
            identity.fixBuyer = "F";
            identity.minage = "18";
            identity.needCheckInfo = "F";
            identity.type = "01";


            string AesKey = WeiPayUtils.GetAesKey();

            dynamic encryInfo = new ExpandoObject();
            encryInfo.mobile = "18579106328";
            encryInfo.name = "潘文敏";
            encryInfo.number = "362228198712100518";
            identity.encryptInfo = EncryptionUtil.AesEncrypt(JsonConvert.SerializeObject(encryInfo), AesKey);
            Console.WriteLine(AesKey);
            Console.WriteLine(JsonConvert.SerializeObject(encryInfo));
            Console.WriteLine(identity.encryptInfo);

            identity.fixBuyer = "T";
            identity.minage = "18";
            identity.type = "01";

            appenddata.identity = identity;
            appenddata.dgtlEnvlp = EncryptionUtil.RsaEncrypt(AesKey, WeiPayConfig.PublicKey, true);
            appenddata.identity = identity;

            #endregion
            m_ScanPay.appendData = appenddata;
            

            var result= HttpPost<M_ScanPay>(WeiPayConfig.ApiUrlScanPay, m_ScanPay);

            Console.WriteLine(string.Format("返回结果：[{0}]", result));

            WeiPayResult weiPayResult=JsonConvert.DeserializeObject<WeiPayResult>(result);

            var resultSignSrc = WeiPayUtils.GetSignData<ScanPayData>(weiPayResult.data);

            bool signMatch = EncryptionUtil.VerifyJavaBouncyCastle(resultSignSrc, WeiPayConfig.PublicKey, weiPayResult.sign, "SHA256withRSA");
            Console.WriteLine("返回结果验证：" + signMatch);
            /*
            dynamic t = JsonConvert.DeserializeObject<dynamic>(result);
            string sign = t.sign;
            dynamic resultData = t.data;

            var dic = (IDictionary<string, object>)resultData;
            //IDictionary<string, object> dic = resultData as IDictionary<string, object>;

            string signSrc = WeiPayUtils.GetSignData(dic);

            bool signMatch = EncryptionUtil.VerifyJavaBouncyCastle(signSrc.ToString(), WeiPayConfig.PublicKey, sign);
            */

        }
    }
}
